<?php
/**
 * $Id: php.php 11 2010-11-24 22:10:53Z LEON $
 * $Author: LEON $
 * $LastChangedDate: 2010-11-24 22:10:53 +0000 (Wed, 24 Nov 2010) $
 * $Revision: 11 $
 */
namespace cmFly\view;
use cmFly\core;
class php implements iView {
    protected $parent;
    protected $vars=Array();
    protected $globals=Array();
    protected $layers=Array();
    protected $main_template_default;
    protected $main_template;
    protected $mode;
    protected $core;
    public function __construct(core\base $core,$dir,iView $parent=NULL,$mode='default') {
	// TODO: Сделать удаление пути к дире, если выполнение модуля, закончилось исключением
	if($parent) {
	    $this->set_parent($parent);
	    $this->main_template_default=&$parent->main_template_default;
	    $this->main_template=&$parent->main_template;
	}
	else {
	    $this->main_template_default='main';
	}
	$this->core=$core;
	$this->set_dir($dir.'/');
	$this->set_mode($mode);
	$this->set_main_template($this->main_template_default);
    }
    public function set_parent(iView $parent) {
	$this->parent=$parent;
    }
    public function set_mode($mode) {
	$this->mode=$mode;
    }
    protected function set_dir($dir) {
	$this->dir=$dir;
    }
    public function set_main_template($tpl) {
	$this->main_template_default=$tpl;
	if($file=$this->tpl_exists($tpl)) {
	    $this->main_template=$file;
	}
	else if($this->parent) {
	    $this->parent->set_main_template($tpl);
	}
	#var_dump($this->parent);
	#if(isset($this->parent)) {
	#	$this->parent->set_main_template($tpl);
	#}
    }
    public function get_main_template() {
	return $this->main_template;
    }
    public function set_global($global_var,$value) {
	if(isset($this->parent)) {
	    return $this->parent->set_global($global_var,$value);
	}
	$this->globals[$global_var]=$value;
    }
    protected function get_globals() {
	if(!isset($this->parent)) {
	    return $this->globals;
	}
	return $this->parent->get_globals();
    }
    protected function get_vars() {
	$vars=$this->vars;
	if(isset($this->parent)) {
	    $vars+=$this->parent->get_vars();
	}
	return $vars;
    }
    public function set_var($var_name,$var_value) {
	$this->vars[$var_name]=$var_value;
    }
    public function get_var($var_name) {
	if(isset($this->vars[$var_name])) {
	    return $this->vars[$var_name];
	}
	return NULL;
    }
    public function set_link($var_name,&$var_value) {
	$this->vars[$var_name]=&$var_value;
    }
    public function tpl_exists($tpl_name) {
	if(file_exists($file=$this->dir.$this->mode.'/'.$tpl_name.'.tpl')) {
	    return $file;
	}
	return false;
    }
    protected function get_full_file_name($tpl_name) {
	if($file=$this->tpl_exists($tpl_name)) {
	    return $file;
	}
	if(!$this->parent || !($full=$this->parent->get_full_file_name($tpl_name))) {
	    throw new \cmFly\system\exception('Can\'t find tpl "'.$tpl_name.'"!');
	}
	return $full;
    }
    public function parse_template($tpl_name,$vars=Array()) {
	ob_start();
	$this->load_template($tpl_name,$vars);
	return ob_get_clean();
    }
    protected function display($___tpl_full_name,$___globals,$___vars) {
	extract($___vars,EXTR_SKIP);
	extract($___globals,EXTR_SKIP);
	include($___tpl_full_name);
    }
    public function exec() {
	$globals=$this->get_globals();
	$vars=$this->get_vars();
	foreach($this->layers as $k=>$v) {
	    if(is_string($v)) {
		$this->display($v,$globals,$vars);
	    }
	    else {
		$v->exec();
	    }
	}
    }
    public function extend($dir,$mode) {
	$view=new self($this->core,$dir,NULL,$mode);
	$view->vars=$this->get_vars();
	$view->globals=$this->get_globals();
	return $view;
    }
    public function layer($dir,$mode='default') {
	$layer=new self($this->core,$dir,$this,$mode);
	$this->layers[]=$layer;
	return $layer;
    }
    protected function load_template($tpl_name,$vars=Array()) {
	$file_name=$this->get_full_file_name($tpl_name);
	if(!is_array($vars)) {
	    $vars=Array('var'=>$vars);
	}
	$globals=$this->get_globals();
	$vars+=$this->get_vars();
	$this->display($file_name, $globals, $vars);
	return '';
    }
    public function render() {
	if(!$this->get_main_template()) {
	    throw new \cmFly\system\exception('Can\'t find main template!');
	}
	$globals=$this->get_globals();
	$vars=$this->get_vars();

	$this->display($this->get_main_template(),$globals,$vars);
    }
    public function insert($tpl_name) {
	$file_name=$this->get_full_file_name($tpl_name);
	$this->layers[]=$file_name;
    }
    public function load_widget($cmd) {
	return $this->core->load_widget($cmd);
    }
}